package py;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.concurrent.ExecutionException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import py.excelHandle.ExcelUtil;
import py.util.ExcelConfig;

public class MainForm
  extends JFrame
{
  private static final long serialVersionUID = 1L;
  
  public MainForm()
  {
//    ExcelConfig.init();
    initComponents();
    setPreferredSize(new Dimension(600, 500));
  }
  
  private File sourceFile = null;
  private JButton execButton;
  private JLabel fileChooseTip;
  private JFileChooser filePathChooser;
  private JButton jButton1;
  private JLabel jLabel2;
  private JScrollPane jScrollPane1;
  /**
 * 
 */
private JButton openButton;
  private JTextArea processArea;
  private JTextField sourcePathText;
  /**
 * 
 */
/**
 * 
 */
private JLabel tipLable;
  
  private void initComponents()
  {
    this.filePathChooser = new JFileChooser();
    this.tipLable = new JLabel();
    this.jScrollPane1 = new JScrollPane();
    this.processArea = new JTextArea();
    this.jLabel2 = new JLabel();
    this.fileChooseTip = new JLabel();
    this.sourcePathText = new JTextField();
    this.openButton = new JButton();
    this.execButton = new JButton();
    this.jButton1 = new JButton();
    
    this.filePathChooser.setCurrentDirectory(null);
    this.filePathChooser.setFileHidingEnabled(false);
//    this.filePathChooser
//      .setFileSelectionMode(1);
    
    setDefaultCloseOperation(3);
    
    this.setTitle("Excel 批处理工具 MD BY PY");
    
    this.tipLable.setText("Excel文件转换");
    
    this.processArea.setColumns(20);
    this.processArea.setRows(5);
    this.jScrollPane1.setViewportView(this.processArea);
    
    this.jLabel2.setText("进度提示");
    
    this.fileChooseTip.setText("Excel源文件夹");
    
    this.openButton.setText("选择文件夹");
    this.openButton.setActionCommand("chooseFilePath");
    this.openButton.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        MainForm.this.chooseFilePath(evt);
      }
    });
    this.execButton.setText("执行");
    this.execButton.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        MainForm.this.execAction(evt);
      }
    });
    this.jButton1.setText("直接进行文件合并");
    this.jButton1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent evt)
      {
        MainForm.this.jButton1ActionPerformed(evt);
      }
    });
    GroupLayout layout = new GroupLayout(
      getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout
      .createParallelGroup(GroupLayout.Alignment.LEADING)
      .addGroup(
      layout.createSequentialGroup()
      .addGroup(
      layout.createParallelGroup(
      GroupLayout.Alignment.LEADING)
      .addGroup(
      layout.createSequentialGroup()
      .addGap(351, 
      351, 
      351)
      .addComponent(
      this.tipLable, 
      -2, 
      388, 
      -2))
      .addGroup(
      layout.createSequentialGroup()
      .addGap(45, 45, 
      45)
      .addComponent(
      this.fileChooseTip)
      .addGap(27, 27, 
      27)
      .addGroup(
      layout.createParallelGroup(
      GroupLayout.Alignment.LEADING)
      .addGroup(
      layout.createSequentialGroup()
      .addComponent(
      this.execButton)
      .addGap(153, 
      153, 
      153)
      .addComponent(
      this.jButton1))
      .addGroup(
      layout.createSequentialGroup()
      .addComponent(
      this.sourcePathText, 
      -2, 
      355, 
      -2)
      .addPreferredGap(
      LayoutStyle.ComponentPlacement.RELATED)
      .addComponent(
      this.openButton))))
      .addGroup(
      layout.createSequentialGroup()
      .addContainerGap()
      .addGroup(
      layout.createParallelGroup(
      GroupLayout.Alignment.LEADING)
      .addComponent(
      this.jScrollPane1, 
      -1, 
      727, 
      32767)
      .addGroup(
      layout.createSequentialGroup()
      .addGap(20, 
      20, 
      20)
      .addComponent(
      this.jLabel2)))))
      .addContainerGap()));
    layout.setVerticalGroup(layout
      .createParallelGroup(GroupLayout.Alignment.LEADING)
      .addGroup(
      layout.createSequentialGroup()
      .addComponent(this.tipLable, 
      -2, 
      31, 
      -2)
      .addGap(46, 46, 46)
      .addGroup(
      layout.createParallelGroup(
      GroupLayout.Alignment.BASELINE)
      .addComponent(this.fileChooseTip)
      .addComponent(
      this.sourcePathText, 
      -2, 
      -1, 
      -2)
      .addComponent(this.openButton))
      .addGap(47, 47, 47)
      .addGroup(
      layout.createParallelGroup(
      GroupLayout.Alignment.BASELINE)
      .addComponent(this.execButton)
      .addComponent(this.jButton1))
      .addGap(34, 34, 34)
      .addComponent(this.jLabel2)
      .addPreferredGap(
      LayoutStyle.ComponentPlacement.RELATED)
      .addComponent(this.jScrollPane1, 
      -2, 
      228, 
      -2)
      .addContainerGap(29, 32767)));
    
    pack();
  }
  
  private void jButton1ActionPerformed(ActionEvent evt)
  {
    String mergePath = this.sourceFile.getAbsolutePath();
    ExcelUtil ex = new ExcelUtil();
    try
    {
      ex.mergeFile(this.processArea, mergePath);
    }
    catch (Exception e)
    {
      this.processArea.append(e.getMessage());
      e.printStackTrace();
    }
  }
  
  /**
 * @param evt
 */
private void execAction(ActionEvent evt)
  {
	ExcelConfig.init();
    if (this.sourceFile != null)
    {
      ExcelUtil ex = new ExcelUtil();
      try
      {
        ex.doWork(this.sourceFile, this.processArea);
      }
      catch (InterruptedException|ExecutionException e)
      {
        this.processArea.append(e.getMessage());
        e.printStackTrace();
      }
    }
  }
  
  private void chooseFilePath(ActionEvent evt)
  {
	this.filePathChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    this.filePathChooser.showDialog(null, "选择");
    this.sourceFile = this.filePathChooser.getSelectedFile();
    this.sourcePathText.setText(this.sourceFile.getAbsolutePath());
    this.processArea.setText("Excel File Path is:" + 
    this.sourceFile.getAbsolutePath() + "\r\n");
  }
  

  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      public void run()
      {
        new MainForm().setVisible(true);
      }
    });
  }
}
